package com.taobao.taobaowidget.test;



import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;

import com.taobao.taobaowidget.tabbar.TabBarView;
import com.taobao.taobaowidget.tabbar.adapter.TabBarImageAdapter;

public class TabBarActivity extends Activity {

    private List<Drawable> mmdata;
    TabBarImageAdapter mAdapter ;
    TabBarView mtb;
    Button mDelete;
    Button mUpdata;
    Button mAdd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabbar_layout);
        mtb =  (TabBarView)findViewById(R.id.tabbar);
        mDelete = (Button)findViewById(R.id.delete);
        mUpdata = (Button)findViewById(R.id.updata);
        mAdd = (Button)findViewById(R.id.add);
        
        mmdata = new ArrayList<Drawable>();
        mAdapter = new TabBarImageAdapter(this, mmdata);
        new Thread(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
              
                Drawable drawable = getResources().getDrawable(R.drawable.icon_app_center);
                mmdata.add(drawable);
                   
              
                drawable = getResources().getDrawable(R.drawable.icon_convenience);
                mmdata.add(drawable);
              
        
                drawable = getResources().getDrawable(R.drawable.icon_lottery);
                mmdata.add(drawable);
                
               
                drawable = getResources().getDrawable(R.drawable.icon_mytaobao);
                mmdata.add(drawable);
                

                drawable = getResources().getDrawable(R.drawable.icon_promotion);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_tmall);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_ju);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_convenience);
                mmdata.add(drawable);
              
                drawable = getResources().getDrawable(R.drawable.icon_lottery);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_mytaobao);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_promotion);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_tmall);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_ju);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_convenience);
                mmdata.add(drawable);
              
                drawable = getResources().getDrawable(R.drawable.icon_lottery);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_mytaobao);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_promotion);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_tmall);
                mmdata.add(drawable);
                drawable = getResources().getDrawable(R.drawable.icon_ju);
                mmdata.add(drawable);
                mAdapter.notifyDataSetChanged();
//                mtb.updataData();
                super.run();
            }
            
        }.start();
        
        
        
        mtb.setAdapter(mAdapter);
//        mtb.smothlyScrollToIndex(4);
        mtb.scroolToIndex(4);
        mtb.setNumColumns(4);
        mtb.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                // TODO Auto-generated method stub
                Log.i("zhanglei", "click item : " + pos);
            }
            
        });
        
        TabBarView mtb2 = (TabBarView)findViewById(R.id.tabbar2);
        mtb2.setAdapter(mAdapter);
        mtb2.setAlign(TabBarView.CENTER);
        mtb2.setSpacing(30);
        mtb2.setIndex(3);
        
        mtb2.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                Log.i("zhanglei", "click item" + arg2);
            }
        });

        mDelete.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(3 < mmdata.size() -1 ){
               
                    
                    mmdata.remove(3);
                    mtb.removeData(3);
                }
            }
            
        });
        
        mUpdata.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(4 < mmdata.size() -1 ){
                    Drawable drawables = getResources().getDrawable(R.drawable.ic_launcher);
                    mmdata.remove(4);
                    mmdata.add(4, drawables);
                    mtb.updataData(4);
                }
            }
            
        });
        
        mAdd.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Drawable drawables = getResources().getDrawable(R.drawable.ic_launcher);
                mmdata.add(2, drawables);
                mtb.addData(2);
            }
            
        });
        
    }

}
